<template>
	<a-card :bordered="false" class="mt-2">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="5">
					<a-form-item label="所属项目" name="projectCode">
						<ProjectSelect v-model:value="searchFormState.projectCode" />
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="所属月份" name="belongingMonth">
						<a-select
							v-model:value="searchFormState.belongingMonth"
							placeholder="请选择所属月份"
							:options="monthOptions"
							allowClear
						/>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table ref="tableRef" :columns="columns" :data="loadData" bordered :scroll="{ x: 'max-content' }">
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon>
							<plus-outlined />
						</template>
						新增
					</a-button>
					<a-button type="primary" @click="initialize">
						<template #icon>
							<SyncOutlined />
						</template>
						初始化
					</a-button>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="ratioRef.showDrawer(record.id)">佣金比例</a>
						<a @click="formRef.onOpen(record)">编辑</a>
						<a @click="advanceRef.onOpen(record)">垫付</a>
					</a-space>
				</template>
				<template
					v-else-if="
						column.dataIndex === 'staffCnt' ||
						column.dataIndex === 'entrustCnt' ||
						column.dataIndex === 'entrustAmt' ||
						column.dataIndex === 'repayTarget' ||
						column.dataIndex === 'totalRepay' ||
						column.dataIndex === 'totalComm' ||
						column.dataIndex === 'totalAdvance' ||
						column.dataIndex === 'netComm' ||
						column.dataIndex === 'commTarget'
					"
				>
					{{ formatNumber(record[column.dataIndex]) }}
				</template>
				<template v-else>
					{{ record[column.dataIndex] }}
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" :key="new Date().getMilliseconds()" @successful="tableRef.refresh(true)" />
	<AdvanceDetail ref="advanceRef" @successful="tableRef.refresh(true)" />
	<Ratio ref="ratioRef" @successful="tableRef.refresh(true)" />
</template>
<script setup>
	import monthlyApi from '@/api/bi/goals/monthly/monthlyApi'
	import Form from '@/views/bi/plan/form.vue'
	import Ratio from '@/views/bi/goals/monthly/ratio.vue'
	import ProjectSelect from '@/components/Tjzy/Project/index.vue'
	import tool from '@/utils/tool'
	import AdvanceDetail from '@/views/bi/goals/monthly/advanceDetail.vue'
	import { message } from 'ant-design-vue'
	import repaymentRecordApi from '@/api/bi/repayment/repaymentRecordApi'
	import planApi from '@/api/bi/plan/planApi'
	import { SyncOutlined } from '@ant-design/icons-vue'

	const tableRef = ref()
	const formRef = ref()
	const advanceRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({
		current: 1,
		size: 10,
		projectCode: undefined,
		belongingMonth: undefined
	})
	const ratioRef = ref()
	const monthOptions = tool.dictList('month')
	const columns = [
		{
			title: '产品名称',
			dataIndex: 'productName'
		},
		{
			title: '项目名称',
			dataIndex: 'projectName'
		},
		{
			title: '所属公司',
			dataIndex: 'deptName'
		},
		{
			title: '案件批次',
			dataIndex: 'batch'
		},
		{
			title: '所属月份',
			dataIndex: 'belongingMonth'
		},
		{
			title: '结佣比例',
			dataIndex: 'commRatio'
		},
		{
			title: '委案金额',
			dataIndex: 'entrustAmt'
		},
		{
			title: '委案户数',
			dataIndex: 'entrustCnt'
		},
		{
			title: '人力',
			dataIndex: 'staffCnt'
		},
		{
			title: '人均载案量',
			dataIndex: 'perEntrustCnt'
		},
		{
			title: '上月委案金额',
			dataIndex: 'lastEntrustAmt'
		},
		{
			title: '上月委案户数',
			dataIndex: 'lastEntrustCnt'
		},
		{
			title: '上月人力',
			dataIndex: 'lastStaffCnt'
		},
		{
			title: '上月人均载案量',
			dataIndex: 'lastPerEntrustCnt'
		},
		{
			title: '分总上报回款',
			dataIndex: 'branchRepay'
		},
		{
			title: '回款任务目标',
			dataIndex: 'repayTarget'
		},
		{
			title: '累计回款',
			dataIndex: 'totalRepay'
		},
		{
			title: '回款差', //分总上报回款-累计回款
			dataIndex: 'repayDiff'
		},
		{
			title: '昨日累计',
			dataIndex: 'yesterdayRepay'
		},
		{
			title: '新增回款',
			dataIndex: 'addRepay'
		},
		{
			title: '任务完成率',
			dataIndex: 'taskCompleteRate'
		},
		{
			title: '上月任务完成率',
			dataIndex: 'lastTaskCompleteRate'
		},
		{
			title: '任务完成率差异',
			dataIndex: 'tcrMomDiff'
		},
		{
			title: '上月累计回款',
			dataIndex: 'lastTotalRepay'
		},
		{
			title: '上月目标',
			dataIndex: 'lastRepayTarget'
		},
		{
			title: '佣金目标',
			dataIndex: 'commTarget'
		},
		{
			title: '累计创拥',
			dataIndex: 'totalComm'
		},
		{
			title: '佣金达成率',
			dataIndex: 'commCompleteRate'
		},
		{
			title: '累计垫付',
			dataIndex: 'totalAdvance'
		},
		{
			title: '净累计创拥',
			dataIndex: 'netComm'
		},
		{
			title: '昨日累计创拥',
			dataIndex: 'yesterdayComm'
		},
		{
			title: '昨日累计垫付',
			dataIndex: 'yesterdayAdvance'
		},
		{
			title: '昨日净创拥',
			dataIndex: 'yesterdayNet'
		},
		{
			title: '上月累计创拥',
			dataIndex: 'lastTotalComm'
		},
		{
			title: '创佣差异',
			dataIndex: 'commMomDiff'
		},
		{
			title: '上月垫付',
			dataIndex: 'lastAdvance'
		},
		{
			title: '上月净累计创拥',
			dataIndex: 'lastNetComm'
		},
		{
			title: '上月结佣比例',
			dataIndex: 'lastCommRatio'
		},
		{
			title: '操作',
			dataIndex: 'action',
			fixed: 'right',
			align: 'center'
		}
	]

	const formatNumber = (number) => {
		return new Intl.NumberFormat('zh-CN').format(number)
	}
	const dataParameter = ref({
		id: ''
	})
	const initialize = () => {
		planApi.initialize().then(() => {
			message.success('初始化成功')
		})
	}

	// 分页条件
	const loadData = (parameter) => {
		searchFormState.value.current = parameter.current
		searchFormState.value.size = parameter.size
		return planApi.pageList(searchFormState.value).then((res) => {
			return res
		})
	}

	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
</script>
